class UploadController < ApplicationController

  def index
    @gameFiles = GameFile.all
  end

  def new
  end

  def create

    publicPath = "#{Rails.root}/public/"



    gameFile = GameFile.new
    gameFile.icon_path = params[:icon_path]
    gameFile.name = params[:name]
    gameFile.index_path = params[:index_path]
    gameFile.state = params[:state].to_i
    gameFile.game_type = params[:game_type].to_i
    gameFile.game_level = params[:game_level].to_i


    if gameFile.save
      gameFile.file_path = gameFile.id
      gameFile.save
      #上传文件保存到 public/test目录下面
      file = params[:file_zip]
      p "-"*10
      if file != nil
        uploader = ZipUploader.new
        uploader.store!(file)
        p uploader.filename
        #解压缩文件
        system("mkdir #{publicPath}/games/#{gameFile.file_path}/")
        system("mv #{publicPath}public/test/#{uploader.filename} #{publicPath}/games/#{gameFile.file_path}/")
        system("unzip #{publicPath}games/#{gameFile.file_path}/#{uploader.filename} -d #{publicPath}/games/#{gameFile.file_path}")
        system("rm -rf #{publicPath}/games/#{gameFile.file_path}/__MACOSX #{publicPath}/games/#{gameFile.file_path}/#{uploader.filename}")
        system("chmod 777 #{publicPath}/games/#{gameFile.file_path}/ -R ")
      end
    end
  end

  def edit
    @gameFile = GameFile.find(params[:id])
  end

  def update
    gameFile = GameFile.find_by(id: params[:id])
    gameFile.icon_path = params[:icon_path]
    gameFile.name = params[:name]
    gameFile.index_path = params[:index_path]
    gameFile.state = params[:state].to_i
    gameFile.game_type = params[:game_type].to_i
    gameFile.game_level = params[:game_level].to_i
    gameFile.save
  end
end
